package com.czl.module_storehouse.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.czl.base.data.bean.tengyun.BorrowBean;
import com.czl.base.data.bean.tengyun.ReceiveBean;
import com.czl.module_base.bean.ListBean;
import com.czl.module_base.databinding.CommonSearchViewBinding;
import com.czl.module_base.fragment.BaseRecyclerViewFragment;
import com.czl.module_base.mvp.presenter.InjectPresenter;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.activity.remand.damage.DamageRecordActivity;
import com.czl.module_storehouse.activity.remand.details.RemandBorrowDetailsActivity;
import com.czl.module_storehouse.activity.remand.details.RemandBorrowNeedActivity;
import com.czl.module_storehouse.activity.remand.details.RemandReceiveDetailsActivity;
import com.czl.module_storehouse.activity.remand.details.RemandReceiveNeedActivity;
import com.czl.module_storehouse.activity.remand.direct.DirectReturnActivity;
import com.czl.module_storehouse.activity.remand.selectedgoods.RemandSelectedGoodsActivity;
import com.czl.module_storehouse.adapter.RemandHomeBorrowAdapter;
import com.czl.module_storehouse.adapter.RemandHomeNeedBorrowAdapter;
import com.czl.module_storehouse.adapter.RemandHomeNeedReceiveAdapter;
import com.czl.module_storehouse.adapter.RemandHomeReceiveAdapter;
import com.czl.module_storehouse.bean.CommonStatusBean;
import com.czl.module_storehouse.bean.RemandBean;
import com.czl.module_storehouse.databinding.CommonButtonBinding;
import com.czl.module_storehouse.databinding.RemandBottomButtonBinding;
import com.czl.module_storehouse.event.DirectDamageEvent;
import com.czl.module_storehouse.event.RemandEvent;
import com.czl.module_storehouse.event.RemandSelectOrderEvent;
import com.czl.module_storehouse.mvp.presenter.RemandHomePresenter;
import com.czl.module_storehouse.mvp.view.RemandHomeView;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xubin
 */
public class RemandHomeFragment extends BaseRecyclerViewFragment<CommonSearchViewBinding> implements RemandHomeView {
    private BaseQuickAdapter<RemandBean, BaseViewHolder> mAdapter;
    private int mRemand;
    @InjectPresenter
    RemandHomePresenter mRemandHomePresenter;
    private boolean mReloadData;

    public static RemandHomeFragment newInstance(int remand) {
        Bundle args = new Bundle();
        args.putInt("remand", remand);
        RemandHomeFragment fragment = new RemandHomeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected CommonSearchViewBinding getBinding(LayoutInflater layoutInflater, ViewGroup container) {
        return CommonSearchViewBinding.inflate(layoutInflater);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        initView();
        initListener();
    }

    @Override
    protected void reload() {
        super.reload();
        loadData(true);
    }

    @Override
    protected void loadMore() {
        super.loadMore();
        loadData(false);
    }

    public void loadData(boolean reload) {
        switch (mRemand) {
            case 0:
                //领用待还
                if (mRemandHomePresenter != null) {
                    mRemandHomePresenter.getNeedReturnReceiveList(reload);
                }
                break;
            case 1:
                //领用已还
                if (mRemandHomePresenter != null) {
                    mRemandHomePresenter.getReturnReceiveList(reload);
                }
                break;
            case 2:
                //借用待还
                if (mRemandHomePresenter != null) {
                    mRemandHomePresenter.getNeedReturnBorrowList(reload);
                }
                break;
            case 3:
                //借用已还
                if (mRemandHomePresenter != null) {
                    mRemandHomePresenter.getReturnBorrowList(reload);
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (mReloadData && isVisibleToUser) {
            mReloadData = false;
            loadData(true);
        }
    }

    public void loadData(boolean reload, int remand) {
        this.mReloadData = reload;
        mRemand = remand;
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(CommonStatusBean event) {
        if (event != null) {
            reload();
        }
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(DirectDamageEvent event) {
        if (event != null) {
            if (mAdapter == null) {
                return;
            }
            reload();
        }
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(RemandSelectOrderEvent event) {
        if (event != null) {
            if (mAdapter == null) {
                return;
            }
            reload();
        }
    }

    @Override
    public <T> void showData(T t) {
        super.showData(t);
        if (t instanceof ListBean) {
            if (mRecyclerViewBinding.recyclerView.getAdapter() == null) {
                mRecyclerViewBinding.recyclerView.setAdapter(mAdapter);
            }
            SmartRefreshLayout refreshLayout = mRecyclerViewBinding.smartRefreshLayout;
            refreshLayout.finishRefresh();
            refreshLayout.finishLoadMore();
            showLoadSuccess();
            List<RemandBean> remandList = new ArrayList<>();
            int total = ((ListBean<?>) t).getTotal();
            boolean hasNextPage = ((ListBean<?>) t).hasNextPage();
            if (mRemand == 0 || mRemand == 1) {
                ListBean<ReceiveBean> list = (ListBean<ReceiveBean>) t;
                if (list.getList() != null) {
                    for (ReceiveBean bean : list.getList()) {
                        RemandBean remandBean = new RemandBean(bean);
                        remandList.add(remandBean);
                    }
                }
            } else {
                ListBean<BorrowBean> list = (ListBean<BorrowBean>) t;
                if (list.getList() != null) {
                    for (BorrowBean bean : list.getList()) {
                        RemandBean remandBean = new RemandBean(bean);
                        remandList.add(remandBean);
                    }
                }
            }
            if (mRemandHomePresenter.getPageNo() == 1) {
                if (remandList.isEmpty()) {
                    hasData = false;
                    setEmptyView(mAdapter);
                    refreshLayout.setEnableLoadMore(false);
                    return;
                }
                hasData = true;
                mAdapter.setList(remandList);
                refreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                hasData = true;
                mAdapter.addData(remandList);
                if (total == mAdapter.getData().size() || !hasNextPage) {
                    refreshLayout.finishLoadMoreWithNoMoreData();
                }
            }
        }
    }

    protected void initView() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        Bundle arguments = getArguments();
        if (arguments != null) {
            mRemand = arguments.getInt("remand");
            switch (mRemand) {
                case 0:
                    RemandBottomButtonBinding bottomButtonBinding = RemandBottomButtonBinding.inflate(getLayoutInflater(), mRecyclerViewBinding.containerBottom, true);
                    bottomButtonBinding.textScan.setOnClickListener(v -> startActivity(new Intent(requireContext(), DirectReturnActivity.class)));
                    //选择物品归还
                    bottomButtonBinding.textSelected.setOnClickListener(v -> startActivity(new Intent(requireContext(), RemandSelectedGoodsActivity.class)));
                    mAdapter = new RemandHomeNeedReceiveAdapter(R.layout.item_remand_need_home, new ArrayList<>(), true);
                    break;
                case 1:
                    CommonButtonBinding buttonBinding = CommonButtonBinding.inflate(getLayoutInflater(), mRecyclerViewBinding.containerBottom, true);
                    buttonBinding.button.setText("查看损坏记录");
                    buttonBinding.button.setOnClickListener(v -> startActivity(new Intent(requireContext(), DamageRecordActivity.class)));
                    mAdapter = new RemandHomeReceiveAdapter(R.layout.item_remand_home, new ArrayList<>());
                    break;
                case 2:
                    bottomButtonBinding = RemandBottomButtonBinding.inflate(getLayoutInflater(), mRecyclerViewBinding.containerBottom, true);
                    bottomButtonBinding.textScan.setOnClickListener(v -> startActivity(new Intent(requireContext(), DirectReturnActivity.class)));
                    //选择物品归还
                    bottomButtonBinding.textSelected.setOnClickListener(v -> startActivity(new Intent(requireContext(), RemandSelectedGoodsActivity.class)));
                    mAdapter = new RemandHomeNeedBorrowAdapter(R.layout.item_remand_home_need_borrow, new ArrayList<>(), true);
                    break;
                default:
                    buttonBinding = CommonButtonBinding.inflate(getLayoutInflater(), mRecyclerViewBinding.containerBottom, true);
                    buttonBinding.button.setText("查看损坏记录");
                    buttonBinding.button.setOnClickListener(v -> startActivity(new Intent(requireContext(), DamageRecordActivity.class)));
                    mAdapter = new RemandHomeBorrowAdapter(R.layout.item_remand_home_borrow, new ArrayList<>());
                    break;
            }
            mRecyclerViewBinding.recyclerView.setAdapter(mAdapter);
        }
    }

    protected void initListener() {
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            RemandBean item = mAdapter.getItem(position);
            goRemandInfoActivity(item);
        });
        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                RemandBean item = mAdapter.getItem(position);
                if (R.id.tv_remand_total == view.getId()) {
                    goDetailsActivity(item);
                }
            }

            private void goDetailsActivity(RemandBean item) {
                RemandEvent event = new RemandEvent();
                event.setRemandBean(item);
                Intent intent = new Intent();
                if (mRemand == 1) {
                    intent.setClass(requireContext(), RemandReceiveDetailsActivity.class);
                } else if (mRemand == 3) {
                    intent.setClass(requireContext(), RemandBorrowDetailsActivity.class);
                }
                EventBus.getDefault().postSticky(event);
                startActivity(intent);
            }
        });
    }

    private void goRemandInfoActivity(RemandBean item) {
        RemandEvent event = new RemandEvent();
        event.setRemandBean(item);
        Intent intent = new Intent();
        switch (mRemand) {
            case 0:
                intent.setClass(getContext(), RemandReceiveNeedActivity.class);
                break;
            case 1:
                intent.setClass(getContext(), RemandReceiveDetailsActivity.class);
                break;
            case 2:
                intent.setClass(getContext(), RemandBorrowNeedActivity.class);
                break;
            case 3:
                intent.setClass(getContext(), RemandBorrowDetailsActivity.class);
                break;
            default:
                break;
        }
        EventBus.getDefault().postSticky(event);
        startActivity(intent);
    }
}
